package com.pb.wkflow.api.controller.assembler;

import com.pb.wkflow.api.controller.request.ProcessInstanceCreateRequest;
import com.pb.wkflow.core.config.Constants;
import com.pb.wkflow.core.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 苗益辉
 * @date 2022/9/19 10:47
 */
public class ProcInstanceAssembler {


    private ProcInstanceAssembler() {

    }

    public static void setGlobalVariablesData(ProcessInstanceCreateRequest request) {
        Map<String,Object> map = request.getGlobalVariables();
        if(map == null) {
            map = new HashMap<>(2);
        }
        //租户id
        if(StringUtils.isNotBlank(request.getTenantId())) {
            map.put("tenantId", request.getTenantId());
        }
        //流程发起人id
        if(StringUtils.isNotBlank(request.getStartUserId())) {
            map.put("startUserId", request.getStartUserId());
        }
        //流程发起人姓名
        if(StringUtils.isNotBlank(request.getStartUserId())) {
            map.put("startUserName", request.getStartUserName());
        }
        //流程发起组织
        if(StringUtils.isNotBlank(request.getStartUserOrg())) {
            map.put("startUserOrg", request.getStartUserOrg());
        }
        //流程发起组织
        if(StringUtils.isNotBlank(request.getStartUserOrg())) {
            map.put("startUserOrgName", request.getStartUserOrgName());
        }
        //流程发起岗位
        if(StringUtils.isNotBlank(request.getStartUserPos())) {
            map.put("startUserPos", request.getStartUserPos());
        }
        //流程发起角色
        if (StringUtils.isNotBlank(request.getStartUserRole())) {
            map.put("startUserRole", request.getStartUserRole());
        }
        if(null != request.getExtensionVariables()) {
            map.put(Constants.EXTENSION_DATA, request.getExtensionVariables());
        } else {
            map.put(Constants.EXTENSION_DATA, new HashMap<>(1));
        }
        request.setGlobalVariables(map);
    }
}
